package com.notes.algo.sx;

public class Solution{

	// 用于获取字符对应的数值
	private static final String constantStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	public static void main(String[] args) {
		java.util.Scanner sc = new java.util.Scanner(System.in);
		// N进制结果
		String iStr = sc.next();
		// N进制
		int iBase = sc.nextInt();
		// N进制转10进制 十进制结果
		int result = baseNToBase10(iStr, iBase);
		System.out.println(result);
		// 10进制转N进制 N进制结果
		String baseNStr = base10ToBaseN(result, iBase);
		System.out.println(baseNStr);
	}

	public static int baseNToBase10(String iStr, int baseN) {
		if (baseN < 2 || baseN > 36) {
			throw new java.lang.RuntimeException("输入进制数不符合");
		}
		int len = iStr.length();
		int r = 0;
		for (int i = len - 1; i >= 0; i --) {
			char curC = iStr.charAt(i);
			int curVal = constantStr.indexOf(curC);
			int tmp = 1;
			for (int j = 0; j < len - i - 1; j ++) {
				tmp *= baseN;
			}

			r += curVal * tmp;

		}

		return r;
	}

	public static String base10ToBaseN(int base10, int baseN) {
		if (baseN < 2 || baseN > 36) {
			throw new java.lang.RuntimeException("输入进制数不符合");
		}

		String result = "";
		// 商值
		int sz = 0;
		while ((sz = (base10 / baseN)) != 0) {
			// 余数
			int ys = base10 % baseN;
			// 余数对应的字符值
			String curVal = constantStr.substring(ys, ys + 1);
			// 拼接到结果
			result = curVal + result;
			base10 = sz;
		}

		// 最后的余数值
		int endYs = base10 % baseN;

		return constantStr.substring(endYs, endYs + 1) + result;
	}
}
